package com.fengba.nanzhi.tabbar01;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.calendarview.weiget.CalendarView;
import com.fengba.nanzhi.calendarview.bean.DateBean;
import com.fengba.nanzhi.calendarview.listener.OnMultiChooseListener;
import com.fengba.nanzhi.calendarview.listener.OnPagerChangeListener;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.myinfo.MyInfo;
import com.fengba.nanzhi.myinfo.WxImg;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MultiChooseActivity extends AppCompatActivity {

    //年
    int year;
    //月
    int month;
    //日
    int day;

    private Button Sbtn01;

    private OKManager manager = OKManager.getInstance();

    private JSONArray jsonArray1;

    private JSONArray jsonArray2;

    private CalendarView calendarView;
    //private TextView chooseDate;


    private TextView Stv02;
    private TextView titles;
    private StringBuilder sbs;
    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        year= calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH)+1;
        day= calendar.get(Calendar.DAY_OF_MONTH);
/*//小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
//分钟
        int minute = calendar.get(Calendar.MINUTE);
//秒
        int second = calendar.get(Calendar.SECOND);*/
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_sign);
        //签到
        Sbtn01  = (Button) findViewById(R.id.Sbtn01);
        Stv02  = (TextView) findViewById(R.id.Stv02);
        titles = (TextView) findViewById(R.id.title);
        inits();
        /**
         * 返回
         */
        ImageButton Sbtn02 = (ImageButton)findViewById(R.id.Sbtn02);
        Sbtn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                //getActivity().overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
            }
        });
        //chooseDate = (TextView) findViewById(R.id.choose_date);
        //chooseDate.setText(sb.toString());
        calendarView = (CalendarView) findViewById(R.id.calendar);
        calendarView
                .setStartEndDate("2018-1", "2020-12")
                .setDisableStartEndDate("2017-10-7", "2019-10-7")
                .setInitDate((year+"-"+month)+"");
        Sbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                if (code.equals("0")) {
                    init_();
                    calendarView.todays();
                    Sbtn01.setBackgroundResource(R.drawable.button_index_css_hui);
                }else
                {
                 Toast.makeText(MultiChooseActivity.this, "您已签到", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void lastMonth(View view) {
        calendarView.lastMonth();
    }

    public void nextMonth(View view) {
        calendarView.nextMonth();
    }
    public void inits()
    {
        String id = null;
        try {
            Object user_info = Preferences.getParam(MultiChooseActivity.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        manager.type2004(id,new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {

                    if (jsonObject.get("code").equals(0)) {
                        Sbtn01.setVisibility(View.INVISIBLE);
                        List<String> list = new ArrayList<String>();
                        sbs = new StringBuilder();
                        code = jsonObject.get("code").toString();
                        jsonArray1 = jsonObject.getJSONArray("data");
                        JSONObject jsonObject1= (JSONObject) jsonArray1.get(0);
                        Stv02.setText(jsonObject1.get("signdays")+"");
                        jsonArray2 = (JSONArray) jsonObject1.get("time");
                        java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
                        list.add("2018-04-20");
                        for (int i=0;i<jsonArray2.length();i++)
                        {
                            Date date =  formatter.parse(jsonArray2.get(i)+"");
                            String stringDate = formatter.format(date);//格式化数据
                            list.add(stringDate);
                        }
                        calendarView = (CalendarView) findViewById(R.id.calendar);
                        calendarView
                                .setMultiDates(list);

                        titles.setText(year + "年" + month + "月");
                        for (String d : list) {
                            sbs.append("选中：" + d + "\n");
                        }
                    } else {
                        Sbtn01.setBackgroundResource(R.drawable.button_index_css_hui);
                        Toast.makeText(MultiChooseActivity.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                    Sbtn01.setVisibility(View.VISIBLE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                Toast.makeText(MultiChooseActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void init_()
    {
        String id = null;
        try {
            Object user_info = Preferences.getParam(MultiChooseActivity.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        manager.type2006(id,new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        Toast.makeText(MultiChooseActivity.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MultiChooseActivity.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(String string) {
                Toast.makeText(MultiChooseActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
